package com.whately.core.base.utils;

import lombok.Getter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HumpTools {

    public enum  SeparatorEnum{

        line("_"),
        mid_line("-"),
        ;
        @Getter
        private String separator;
        SeparatorEnum(String separator){
            this.separator = separator;
        }

    }

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /** 下划线转驼峰 */
    public static String lineToHump(String str,SeparatorEnum split) {
        Pattern linePattern = Pattern.compile(split.getSeparator()+"(\\w)");
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /** 驼峰转下划线 */
    public static String humpToLine(String str,SeparatorEnum split) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, split.getSeparator()+ matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);

        String changed =  sb.toString();
        if(changed.startsWith(split.getSeparator())){
            return changed.substring(split.getSeparator().length());
        }
        return changed;
    }


}
